package com.exam.controller;


import com.exam.entity.ApiResult;
import com.exam.entity.Login;
import com.exam.entity.Register;
import com.exam.entity.User;
import com.exam.serviceimpl.RegisterServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RegisterController {

    @Autowired
    RegisterServiceImpl registerService;

    @PostMapping("/register")
    public ApiResult register(@RequestBody Register register) {

        int num = registerService.userRegister(register);
        if (num != 0) {
            return ApiResultHandler.buildApiResult(200, "请求成功", register);
        }
        return ApiResultHandler.buildApiResult(400, "用户名已存在", null);
    }

    @PostMapping("/email")
    public ApiResult sendCodeToEmail(@RequestBody Register register) {

        String code = registerService.sendCodeToEmail(register);
        if (code != null) {
            return ApiResultHandler.buildApiResult(200, code, register);
        }
        return ApiResultHandler.buildApiResult(400, "验证码发送失败", null);
    }

    @PostMapping("/phone")
    public ApiResult sendCodeToPhone(@RequestBody Register register) {

        String code = registerService.sendCodeToPhone(register);
        if (code != null) {
            return ApiResultHandler.buildApiResult(200, code, register);
        }
        return ApiResultHandler.buildApiResult(400, "验证码发送失败", null);
    }

    @PostMapping("/resetByEmail")
    public ApiResult resetByEmail(@RequestBody Register register) {

        int num = registerService.updateByEmail(register);
        if (num != 0) {
            return ApiResultHandler.buildApiResult(200, "信息更新成功", register);
        }
        return ApiResultHandler.buildApiResult(400, "更新失败", null);
    }

    @PostMapping("/resetByPhone")
    public ApiResult resetByPhone(@RequestBody Register register) {

        int num = registerService.updateByPhone(register);
        if (num != 0) {
            return ApiResultHandler.buildApiResult(200, "信息更新成功", register);
        }
        return ApiResultHandler.buildApiResult(400, "更新失败", null);
    }
}
